﻿using System;
using System.Collections.Generic;
using System.Text;
using com.dz.ct.common;
using System.Text.RegularExpressions;

namespace com.dz.ct.components.SmallSite.www.huaixiu.net
{
    public class SmallSiteObject : ISmallSiteObject
    {
        //http://www.huaixiu.net/files/article/sort/0/{0}.htm
        /// <summary>
        ///收集某个站点的小说信息
        /// </summary>
        /// <param name="i_book_list_url"></param>
        /// <returns>小说章节地址及小说名称</returns>
        public Dictionary<string, string> CollectBookList(string i_book_list_url)
        {
            string t_reg = "<td[^<>]*?class=[\'\"]?odd[\'\"]?><a[^<>]*?hrefs*=s*[\'\"]*[^\"\']*[\'\"]*[^<>]*?>(.*?)</a></td> \\s*?<td^<>]*?>\\[<a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>.*?</a>";
            int t_chpater_list_url_index = 2;
            int t_book_name_index = 1;
            Encoding t_site_encoding = Encoding.GetEncoding("gbk");

            Dictionary<string, string> t_dic = null;
            try
            {
                string t_html = NetSiteCatchManager.ReadUrl(i_book_list_url, t_site_encoding);

                if (!string.IsNullOrEmpty(t_html))
                {

                    MatchCollection t_ma = Regex.Matches(t_html, t_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_ma != null)
                    {
                        t_dic = new Dictionary<string, string>();

                        foreach (Match t_mc in t_ma)
                        {
                            if (!t_dic.ContainsKey(t_mc.Groups[t_book_name_index].Value.Trim()))
                            {
                                t_dic.Add(t_mc.Groups[t_book_name_index].Value.Trim(), t_mc.Groups[t_chpater_list_url_index].Value.Trim());
                            }
                        }
                    }
                }

                return t_dic;
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取3zcn小说名称和章节url失败" + ex.ToString());
                return null;
            }
        }
    }
}
